﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Farigola.Engine.SystemData.Entity.inst;
using Farigola.Engine.SystemData.Entity.Type;

namespace Farigola.Engine.TestUnit
{
    [TestFixture]
    public class EntityFinderTestUnit
    {
	    /**
	     * find the type string by the name.
	     */
	    [Test]
	    public void testFindTypeStringByName() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    EntTypeBase typeBaseString= entFinder.FindEntTypeBaseByName(EntityFinder.FindSystemUserType.All, "string");
		    Assert.IsNotNull(typeBaseString, "The type string should be found by the name.");

		    // convert to a type string to check it
		    Assert.IsTrue((typeBaseString is EntTypeString), "The ent type should be 'string'.");
	    }
	
	    /**
	     * find the type string by the name, more easy.
	     */
	    [Test]
	    public void testFindTypeStringByNameEasy() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    EntTypeBase typeBaseString= entFinder.FindEntTypeBaseByName("string");
		    Assert.IsNotNull(typeBaseString,"The type string should be found by the name.");

		    // convert to a type string to check it
		    Assert.IsTrue((typeBaseString is EntTypeString),"The ent type should be 'string'.");
	    }
	
	    /**
	     * find the type folder by the name, more easy.
	     */
	    [Test]
	    public void testFindTypeFolderByNameEasy() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    EntTypeBase typeBaseFolder= entFinder.FindEntTypeBaseByName("folder");
		    Assert.IsNotNull(typeBaseFolder,"The type folder should be found by the name.");
		
		    // convert to a type string to check it
		    Assert.IsTrue((typeBaseFolder is EntTypeFolder), "The ent type should be 'folder'.");
	    }
	
	    /**
	     * basic test of the EntityFinder.
	     */
	    [Test]
	    public void testPrjFindInstFolderByName() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // create folder under the root 
		    EntInstFolder fold = prj.createEntInstFolder(prj.GetRootUserFolder(), "fold");
		    Assert.IsNotNull(fold, "The folder 'fold' should be created.");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    // find it by the name, scan recursilvy
		    EntInstFolder foldfind =entFinder.FindEntInstFolderByName("fold");
		    Assert.IsNotNull(foldfind, "The folder 'fold' should be found.");
		
		    // check the folder name
		    Assert.AreEqual("fold", foldfind.GetName(), "The name of the folder should be 'fold'.");
	    }
	
	    /**
	     * 
	     */
	    [Test]
	    public void testPrjFindInstFolderByNameWrong() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    // try to find a not existing folder
		    EntInstFolder foldNotExistsfind =entFinder.FindEntInstFolderByName("foldNotExists");
		    Assert.IsNull(foldNotExistsfind, "The folder 'foldNotExists' should not be found.");
	    }
	
	
	    /**
	     * 
	     */
	    [Test]
	    public void testPrjFindInstFolderInFolderByName() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // create folder under the root 
		    EntInstFolder fold = prj.createEntInstFolder(prj.GetRootUserFolder(), "fold");
		    EntInstFolder foldChild = prj.createEntInstFolder(fold, "foldChild");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    // find the child folder
		    EntInstFolder foldChildfind =entFinder.FindEntInstFolderByName("foldChild");
		    Assert.IsNotNull(foldChildfind, "The folder 'foldChild' should be found.");

		    // check the folderChild name
		    Assert.AreEqual("foldChild", foldChildfind.GetName(),"The name of the folder should be 'foldChild'.");
	    }

	    /**
	     * 
	     */
	    [Test]
	    public void testPrjFindInstFolderByNameUnderRootFolderSystem() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // get the 
		    EntInstFolder rootFoldSystem = prj.GetRootSystemFolder();
		
		    // create folder under the root system
		    EntInstFolder fold = prj.createEntInstFolder(rootFoldSystem, "fold");
		
		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    // find the child folder, by default, start find in the root folder user
		    EntInstFolder foldFind =entFinder.FindEntInstFolderByName("fold");
		    Assert.IsNull(foldFind, "The folder 'fold' should not be found from the root foler user, is in the root folder system.");
		
		    // now start find from the root folder
		    EntityBase ent= entFinder.FindEntityByName(EntityFinder.FindSystemUserType.All, prj.GetRootFolder(), "fold",true);
		    Assert.IsNotNull(ent, "The folder 'fold' should be found from the root folder.");
		    Assert.IsTrue((ent is EntInstFolder), "The entity should the folder 'fold', found from the root folder.");			
		
		    // start find from the root folder system
		    ent= entFinder.FindEntityByName(EntityFinder.FindSystemUserType.All, prj.GetRootSystemFolder(), "fold",true);
		    Assert.IsNotNull(ent, "The folder 'fold' should be found from the root folder system.");
		    Assert.IsTrue((ent is EntInstFolder), "The entity should the folder 'fold', found from the root folder system.");		
	    }
	
	    /**
	     * basic test of the EntityFinder.
	     */
	    [Test]
	    public void testPrjFindInstFolderById() {
            Engine engine = EngineCommonTestUnit.CreateInitEngine();
            Workspace wksp = engine.GetWorkspace();
		
		    // create a project
		    Project prj= wksp.CreateActiveProject("prj");

		    // create folder under the root 
		    EntInstFolder fold = prj.createEntInstFolder(prj.GetRootUserFolder(), "fold");

		    // get the tool to find entities in the project
		    EntityFinder entFinder= prj.GetEntityFinder();
		
		    // find it by the name, scan recursilvy
		    EntInstFolder foldfind =entFinder.FindEntInstFolderById(fold.GetId());
		    Assert.IsNotNull(foldfind, "The folder 'fold' should be found.");
		
		    // check the folder name
		    Assert.AreEqual("fold", foldfind.GetName(), "The name of the folder should be 'fold'.");
	    }
	
    }
}
